import re
__author__="Poplar"
__date__ ="$2010-2-20 2:16:49$"

import urllib2
import urllib
import os.path
from BeautifulSoup import BeautifulSoup

def decode(html):
    return html.decode('EUC-JP').encode('utf-8')

def findFolderName(soup):
    return soup.find('title').string.split(' ')[-1]

def buildfileName(filePath, fileName):
    return filePath.join(fileName)

def writeLog(filePath, result):
    try:
        op = open(filePath + 'log.txt', "wb")
        op.write(result)
    except:
        op.close()

def download(url, downloadFolder):
    result = url + "\n"
    alldone = 1;
    try:
        page = urllib2.urlopen(beginurl)
    except:
        print '[ failed ]'
    else:
        result += '[ success ]\n'
        print '[ success ]'
        html = decode(page.read())
        soup = BeautifulSoup(html)
        name = findFolderName(soup)
        links = soup.findAll('a', href=re.compile('^http://blog-imgs-31-origin\.fc2\.com.+?\.jpg$'))#target="_blank")
        if(len(links) == 0): return '[ null ]'
        filePath = downloadFolder + name + "(" + str(len(links)) + "P)" + "/"
        if not os.path.isdir(filePath):
            os.mkdir(filePath)
        time = 0
        for a in links:
            url = a['href']
            try:
                time += 1
                fileName = '%03d.jpg' %(time) #url.split('/')[-1]
                urllib.urlretrieve(url, filePath + fileName)
                msg = url + " to " + fileName + " ok!\n"
                print msg
                result += msg
            except:
                msg = url+" not ok!\n"
                print msg
                result += msg
                alldone = 0
        if(alldone) :
            msg = '[ all done ]'
        else :
            msg = '[ something wrang ]'
        print msg
        result += msg
        page.close

        writeLog(filePath, result)
        return msg


if __name__ == "__main__":
    result = ""
    downloadFolder = 'E:/image/photomovie.blog92.fc2.com/'
    start = 287;
    end = 452;
    for num in range(start, end+1):
        beginurl = 'http://photomovie.blog92.fc2.com/blog-entry-'+str(num)+'.html'
        msg = beginurl + '\t'
        print msg
        result += msg
        result += download(beginurl, downloadFolder+'%03d ' %(num)) + "\n"
        writeLog(downloadFolder + '%03d-%03d-' %(start, end), result)

